package com.gaga.console.base.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Component;

/**
 * ECache操作实体类
 */
@Component
public class ECacheUtil {
    private static EhCacheCacheManager ehCacheCacheManager;

    public static void addCache(String code) {
        try {
            ehCacheCacheManager.getCacheManager().addCache(code);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Cache getCache(String code) {
        return ehCacheCacheManager.getCache(code);
    }

    public static void put(String cacheName, String key, Object value) {
        Cache cache = ehCacheCacheManager.getCache(cacheName);
        cache.put(key, value);
    }

    public static Object get(String cacheName, String key, Class<?> arg1) {
        Cache cache = ehCacheCacheManager.getCache(cacheName);
        return cache == null ? null : cache.get(key, arg1);
    }


    public static void remove(String cacheName, String key) {
        Cache cache = ehCacheCacheManager.getCache(cacheName);
        cache.evict(key);
    }

    public static void clear(String cacheName) {
        Cache cache = ehCacheCacheManager.getCache(cacheName);
        cache.clear();
    }


    @Autowired(required = true)
    public void setEhCacheCacheManager(EhCacheCacheManager ehCacheCacheManager) {
        ECacheUtil.ehCacheCacheManager = ehCacheCacheManager;
    }


}
